package Tree.Recursive;

import Utils.TreeNode;

/**
 * 平衡二叉树:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
 * Lettcode 110: https://leetcode-cn.com/problems/balanced-binary-tree/
 */
public class isBalanced {
    boolean res = true;

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        maxDepth(root);
        return res;
    }

    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        if (Math.abs(left - right) > 1) {
            res = false;
        }
        return Math.max(left, right) + 1;
    }
}
